import PySimpleGUI as sg
from PIL import Image, ImageDraw, ImageTk, ImageFont
import cv2

# All the stuff inside your window.
layout= [
            [sg.Text('输入姓名:'), sg.InputText(key="name")],
            [sg.Image(filename="logo.png", size=(640,480), pad=(10,10), key="image")],
            [sg.Button("拍照"), sg.Button('确认'), sg.Button('取消')] ]

sg.theme('DarkTeal10')   # Add a touch of color
window = sg.Window('人脸注册系统', layout, font="Any 20", location=(600, 300),  finalize=True)
#sg.PopupOKCancel("提示")
# Event Loop to process "events" and get the "values" of the inputs
capture = cv2.VideoCapture(0)
font = ImageFont.truetype(font='simsun.ttc', size=40)
def refresh(element):
    print("3")

while True:
    event, values = window.read(0)
    if event == sg.WIN_CLOSED:
        break
    elif  event == '取消': # if user closes window or clicks cancel
        window.FindElement("name").update(value="")

    if len(values['name']) > 0:
        print('You entered ', values["name"])
    #print(eval(values[0]))
    ret, frame = capture.read()
    im = Image.fromarray(frame)
    draw = ImageDraw.Draw(im)
    draw.text(xy=(100, 50), text='确保人脸在红色方框内', fill=(255, 0, 0), font=font)
    draw.rectangle(xy=(160, 120, 480, 360), fill=None, outline="red", width=2)
    tkimage = ImageTk.PhotoImage(image=im)
    window.FindElement("image").update(data=tkimage)

window.close()
 
